package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.base.response.JsonResult;
import cn.tedu.egmvc3.base.response.StatusCode;
import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserAddParam;
import cn.tedu.egmvc3.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
@Api(tags = "01.用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("add")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 10)
    public JsonResult add(UserAddParam userAddParam) {
       // System.out.println("userAddParam = " + userAddParam);
        log.debug("userAddParam = " + userAddParam);
        User user = new User();
        //复制属性
        BeanUtils.copyProperties(userAddParam,user);
        //处理其他属性
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int rows =userMapper.insertUser(user);
        if (rows == 0) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
            return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list() {
        log.debug("UserController.list()");
        List<UserListVO> list = userMapper.list();
        System.out.println("list = " + list);
        return JsonResult.ok(list);
    }

    /**
     * 3.删除用户功能
     */
    @PostMapping("del")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 30)
    public JsonResult del(Long id){
       // System.out.println("id = " + id);
        log.debug("id = " + id);
        int i = userMapper.del(id);
        if (i>0){
            return JsonResult.ok();
        }else {
            log.debug("id = " + id);
            throw new IllegalArgumentException("id值非法");
           // return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    /**
     * 4.更新指定用户功能
     */
    @PostMapping("update")
    @ApiOperation("更新用户")
    @ApiOperationSupport(order = 40)
    public JsonResult update(UserUpdateParam userUpdateParam){
        //System.out.println("userUpdateParam = " + userUpdateParam);
        log.debug("userUpdateParam = " + userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int i = userMapper.update(user);

        if (i>0){
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
